package com.dmxtechnologies.community.ejb.interfaces.impl;

import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.dmxtechnologies.community.ejb.interfaces.IndexRealTimeBroadcastRemote;
import com.dmxtechnologies.community.rar.api.CMSBaseRequest;
import com.dmxtechnologies.community.rar.api.CMSBaseResponse;
import com.dmxtechnologies.community.rar.api.CMSConnection;
import com.dmxtechnologies.community.rar.api.CMSConnectionFactory;

/**
 * Session Bean implementation class IndexRealTimeBroadcast
 */
@Singleton
@Startup
public class IndexRealTimeBroadcast implements IndexRealTimeBroadcastRemote {
	private static final Logger log = Logger.getLogger("RealTimeBroadcast");
	@Resource(lookup = "java:global/env/eis/DMXCMSConnectionFactory")
	private CMSConnectionFactory connectionFactory;
	
	private CMSBaseResponse baseResponse = null;

	/**
     * Default constructor. 
     */
    public IndexRealTimeBroadcast() {
    }
    
	@Schedule(minute = "*/55", hour = "*", persistent = false)
	public void timer() throws Exception {
		log.info("enter");
		//229748_00406001_json_229748_1
		CMSBaseRequest request = new CMSBaseRequest();
		request.setCommunity("229748");
		request.setPreColumns("00406001");
		request.setTemplateExample("json");
		request.setNextColumns("229748");
		request.setPagination(1);
		log.info("[ResourceAccessBean] Getting connection from the RA");
			try (CMSConnection connection = connectionFactory.getConnection()){
				baseResponse = connection.getListRequestToCMS(request);
				log.info("invoke success");
			}
	}

	@Override
	public CMSBaseResponse getCarouselList() throws Exception {
		if(baseResponse == null){
			timer();
		}
		return baseResponse;
	}
}
